package top.koqia.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    private ApiInfo apiInfo() {
        Contact contact = new Contact("KoQia","#","769970378@qq.com");
        return new ApiInfoBuilder().title("接口文档")
                .description("文档描述")
                .version("1.0.0")
                .contact(contact)
                .build();
    }

    @Bean
    public Docket createRestApi(Environment environment) {
//        Profiles dev = Profiles.of("pro");
        boolean b = true /*environment.acceptsProfiles(dev)*/;

        ParameterBuilder parameterBuilder = new ParameterBuilder();
        ArrayList<Parameter> pars = new ArrayList<>();
        parameterBuilder.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(parameterBuilder.build());
        return new Docket(DocumentationType.SWAGGER_2).enable(b)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("top.koqia")) //这里写的是API接口所在的包位置
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }

        private static List<ApiKey> unifiedAuth(){
        List<ApiKey> apiKeys = new ArrayList<>();
        apiKeys.add(new ApiKey("Authorization","token","header"));
        return apiKeys;
    }


}
